package com.schoolai.ai.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.schoolai.entity.SchoolAiDietaryRequirements;
import com.schoolai.feign.IFeignSchoolDietaryRequirementsontroller;
import com.schoolai.util.base.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.QuestionAnswerAdvisor;
import org.springframework.ai.document.Document;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.SimpleVectorStore;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Copyright(C),2019-2025，XX公司
 * FileName:DietaryRequirementsController
 * Author:bobby
 * 创建时间：2025/9/24 15:16
 * Description:午餐模块
 * History:
 * <auth>        <time>       <version>       <desc>
 * 作者          修改时间       版本号         描述
 */
@Slf4j
@RestController
@Tag(name = "午餐AI模块", description = "午餐AI模块")
public class DietaryRequirementsController {
    /// 嵌入式模型
    @Autowired
    EmbeddingModel embeddingModel;
    @Autowired
    IFeignSchoolDietaryRequirementsontroller iFeignSchoolAiDietaryRequirementsController;
    private final ChatClient chatClient;
    public DietaryRequirementsController(ChatClient.Builder builder) {
        this.chatClient = builder.build();
    }

    @Operation(summary = "午餐推荐")
    @ApiOperationSupport(order = 1)
    @GetMapping("/lunchRecommendation")
    public Result lunchRecommendation() {
        /// 1、读取午餐要求内容
        List<SchoolAiDietaryRequirements> SchoolAiDietaryRequirementsList = iFeignSchoolAiDietaryRequirementsController.list();
        if(SchoolAiDietaryRequirementsList==null && SchoolAiDietaryRequirementsList.size()==0) {
            log.info("还未提出要求");
            return Result.ok("还未提出要求");
        }
        StringBuffer recommendationBuff = new StringBuffer("乌当区实验小学餐饮要求：");
        SchoolAiDietaryRequirementsList.forEach(e->{
            recommendationBuff.append(e.getContent()).append("\n");
        });
        /// 2、使用嵌入模型转化向量
        VectorStore vectorStore = SimpleVectorStore.builder(embeddingModel).build();
        Document document1 = Document.builder()
                .text(recommendationBuff.toString()).build();
        //存储向量(内部会自动向量化)
        vectorStore.add(List.of(document1));
        /// 3、使用deepseek模型回答
        String aiRecommendation = chatClient.prompt()
                .system("你是一个智能助手")
                .user("根据乌当区实验小学餐饮要求推荐菜谱")
                //配置一个日志拦截器，方便查看Rag查到内容
                .advisors(
                        //向量数据库高级查询，一般都只调topK和similarityThreshold的阈值来让文档更精确
                        QuestionAnswerAdvisor.builder(vectorStore)
                                .searchRequest(SearchRequest.builder()
                                        .topK(5)
                                        .similarityThreshold(0.5)
                                        .build()
                                ).build()
                )
                .call()
                .content();
        return Result.ok(aiRecommendation);
    }
}
